package bb.lanxing.activity.bluetooth.light;

import android.view.View;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.SeekBar;
import android.widget.TextView;
import androidx.lifecycle.LifecycleOwnerKt;
import com.garmin.fit.MonitoringReader;
import com.umeng.analytics.pro.an;
import bb.lanxing.R;
import bb.lanxing.activity.bluetooth.base.BaseSensorActivity;
import bb.lanxing.databinding.ActivityLightDeviceBinding;
import bb.lanxing.devices.utils.XZDeviceHelper;
import bb.lanxing.lib.devices.ble.ble.characteristic.light.LightBeamMode;
import bb.lanxing.lib.devices.ble.ble.characteristic.light.LightMachineStatus;
import bb.lanxing.lib.devices.ble.ble.characteristic.light.LightMode;
import bb.lanxing.lib.devices.ble.light.LightDeviceController;
import bb.lanxing.util.ui.ViewExtensionsKt;
import kotlin.Metadata;
import kotlin.Unit;
import kotlin.jvm.functions.Function0;
import kotlin.jvm.internal.DefaultConstructorMarker;
import kotlin.jvm.internal.Intrinsics;
import kotlinx.coroutines.flow.MutableStateFlow;

public final class LightDeviceActivity extends BaseSensorActivity<ActivityLightDeviceBinding> {
    private boolean isLightBeamCheckedFromUser;
    private boolean isLightIntensityCheckedFromUser;
    private boolean isLightModeCheckedFromUser;
    private final int layoutId;

    public LightDeviceActivity() {
        this(0, 1, null);
    }

    public LightDeviceActivity(int i) {
        this.layoutId = i;
        this.isLightModeCheckedFromUser = true;
        this.isLightBeamCheckedFromUser = true;
        this.isLightIntensityCheckedFromUser = true;
    }

    public /* synthetic */ LightDeviceActivity(int i, int i2, DefaultConstructorMarker defaultConstructorMarker) {
        this((i2 & 1) != 0 ? R.layout.activity_light_device : i);
    }

    @Override
    public int getLayoutId() {
        return this.layoutId;
    }

    public final boolean isLightModeCheckedFromUser() {
        return this.isLightModeCheckedFromUser;
    }

    public final void setLightModeCheckedFromUser(boolean z) {
        this.isLightModeCheckedFromUser = z;
    }

    public final boolean isLightBeamCheckedFromUser() {
        return this.isLightBeamCheckedFromUser;
    }

    public final void setLightBeamCheckedFromUser(boolean z) {
        this.isLightBeamCheckedFromUser = z;
    }

    public final boolean isLightIntensityCheckedFromUser() {
        return this.isLightIntensityCheckedFromUser;
    }

    public final void setLightIntensityCheckedFromUser(boolean z) {
        this.isLightIntensityCheckedFromUser = z;
    }

    @Override
    public void initView(final ActivityLightDeviceBinding binding) {
        Intrinsics.checkNotNullParameter(binding, "binding");
        super.initView(binding);

        binding.imgLightSwitcher.setOnClickListener(view -> switchLightStateOnOrOff());

        binding.radioGroupLightMode.setOnCheckedChangeListener(this::onClickLightMode);

        binding.radioGroupLightBeam.setOnCheckedChangeListener(this::onClickLightBeam);

        binding.seekbarIntensity.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int i, boolean z) {
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {
            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {
                if (!LightDeviceActivity.this.isLightIntensityCheckedFromUser()) {
                    LightDeviceActivity.this.setLightIntensityCheckedFromUser(true);
                    return;
                }
                SeekBar seekBar2 = binding.seekbarIntensity;
                Intrinsics.checkNotNullExpressionValue(seekBar2, "binding.seekbarIntensity");
                if (ViewExtensionsKt.isFastClick(seekBar2, ViewExtensionsKt.getFAST_CLICK_THRSHOLD())) {
                    return;
                }
                LightDeviceActivity.this.setLightIntensity(seekBar != null ? seekBar.getProgress() : 0);
            }
        });

        binding.unbindBtn.setOnClickListener(view -> unBoundDevice());
    }

    public void onClickLightMode(RadioGroup lightMode, int i) {
        if (!isLightModeCheckedFromUser) {
            isLightModeCheckedFromUser = true;
            return;
        }
        RadioButton always = lightMode.findViewById(R.id.rb_light_mode_always);
        RadioButton flash = lightMode.findViewById(R.id.rb_light_mode_flash);
        RadioButton flash3Hz = lightMode.findViewById(R.id.rb_light_mode_smooth_flash);
        View viewById = lightMode.findViewById(i);
        if (viewById.equals(always)) {
            setLightMode(LightMode.LightOn);
        } else if (viewById.equals(flash)) {
            setLightMode(LightMode.SOSFlash);
        } else if (viewById.equals(flash3Hz)) {
            setLightMode(LightMode.Flash3Hz);
        }
    }


    public void onClickLightBeam(RadioGroup lightBeam, int i) {
        if (!isLightBeamCheckedFromUser) {
            isLightBeamCheckedFromUser = true;
            return;
        }

        RadioButton high = lightBeam.findViewById(R.id.rb_light_beam_high);
        RadioButton low = lightBeam.findViewById(R.id.rb_light_beam_low);
        View viewById = lightBeam.findViewById(i);
        if (viewById.equals(high)) {
            setLightBeam(LightBeamMode.HighBeam);
        } else if (viewById.equals(low)) {
            setLightBeam(LightBeamMode.LowBeam);
        }
    }


    public final void onDeviceValid() {
        MutableStateFlow<LightMachineStatus> lightMachineStatusFlow;
        LightDeviceController lightDeviceController = XZDeviceHelper.getLightDeviceController(getDeviceAddress());
        LightDeviceActivity lightDeviceActivity = this;
        LightMachineStatus lightMachineStatus = null;
//        LifecycleOwnerKt.getLifecycleScope(lightDeviceActivity).launchWhenResumed(new LightDeviceActivity$onDeviceValid$1(XZDeviceHelper.getBatteryController(getDeviceAddress()), this, null));
//        LifecycleOwnerKt.getLifecycleScope(lightDeviceActivity).launchWhenResumed(new LightDeviceActivity$onDeviceValid$2(lightDeviceController, this, null));
        if (lightDeviceController != null && (lightMachineStatusFlow = lightDeviceController.getLightMachineStatusFlow()) != null) {
            lightMachineStatus = lightMachineStatusFlow.getValue();
        }
        setUpUIByLightMachineStatus(lightMachineStatus);
//        if (lightDeviceController != null) {
//            lightDeviceController.requestControl(LightDeviceActivity$onDeviceValid$3.INSTANCE, LightDeviceActivity$onDeviceValid$4.INSTANCE);
//        }
    }

    @Override
    public void onDeviceConnecting() {
        super.onDeviceConnecting();
        setUILightState(LightDeviceState.UNCONNECTED);
    }

    @Override
    public void onDeviceConnected() {
        super.onDeviceConnected();
        LightDeviceController lightDeviceController = XZDeviceHelper.getLightDeviceController(getDeviceAddress());
        if (!lightDeviceController.isInitDone()) {
            lightDeviceController.setInitDoneCallBack(new LightDeviceActivity$onDeviceConnected$1(this, lightDeviceController));
        } else {
            onDeviceValid();
        }
    }

    @Override
    public void onDeviceDisConnected() {
        super.onDeviceDisConnected();
        setUILightState(LightDeviceState.UNCONNECTED);
    }

    public final void setUILightState(LightDeviceState lightDeviceState) {
        Intrinsics.checkNotNullParameter(lightDeviceState, "lightDeviceState");
//        BuildersKt__Builders_commonKt.launch$default(LifecycleOwnerKt.getLifecycleScope(this), Dispatchers.getMain(), null, new LightDeviceActivity$setUILightState$1(this, lightDeviceState, null), 2, null);
    }

    public final void setUpUIByLightMachineStatus(LightMachineStatus lightMachineStatus) {
//        BuildersKt__Builders_commonKt.launch$default(LifecycleOwnerKt.getLifecycleScope(this), Dispatchers.getMain(), null, new LightDeviceActivity$setUpUIByLightMachineStatus$1(this, lightMachineStatus, null), 2, null);
    }

    private final void switchLightStateOnOrOff() {
        LightMachineStatus mo4477getValue;
        LightDeviceController lightDeviceController = XZDeviceHelper.getLightDeviceController(getDeviceAddress());
        if (lightDeviceController != null) {
            MutableStateFlow<LightMachineStatus> lightMachineStatusFlow = lightDeviceController.getLightMachineStatusFlow();
            boolean z = true;
            if (lightMachineStatusFlow == null || (mo4477getValue = lightMachineStatusFlow.getValue()) == null || !mo4477getValue.isLightOn()) {
                z = false;
            }
            if (z) {
                lightDeviceController.setLightMode(LightMode.Off, LightDeviceActivity$switchLightStateOnOrOff$1$1.INSTANCE, new LightDeviceActivity$switchLightStateOnOrOff$1$2(this));
            } else {
                lightDeviceController.setLightMode(LightMode.LightOn, LightDeviceActivity$switchLightStateOnOrOff$1$3.INSTANCE, new LightDeviceActivity$switchLightStateOnOrOff$1$4(this));
            }
        }
    }

    private final void setLightMode(LightMode lightMode) {
        LightDeviceController lightDeviceController = XZDeviceHelper.getLightDeviceController(getDeviceAddress());
        if (lightDeviceController != null) {
            lightDeviceController.setLightMode(lightMode, LightDeviceActivity$setLightMode$1.INSTANCE, new LightDeviceActivity$setLightMode$2(this));
        }
    }

    private final void setLightBeam(LightBeamMode lightBeamMode) {
        LightDeviceController lightDeviceController = XZDeviceHelper.getLightDeviceController(getDeviceAddress());
        if (lightDeviceController != null) {
            lightDeviceController.setLightBeam(lightBeamMode, LightDeviceActivity$setLightBeam$1.INSTANCE, new LightDeviceActivity$setLightBeam$2(this));
        }
    }

    public final void setLightIntensity(int i) {
        LightDeviceController lightDeviceController = XZDeviceHelper.getLightDeviceController(getDeviceAddress());
        if (lightDeviceController != null) {
            lightDeviceController.setLightIntensity(i, LightDeviceActivity$setLightIntensity$1.INSTANCE, new LightDeviceActivity$setLightIntensity$2(this));
        }
    }

    @Override
    public void onGetDeviceFirmwareVersion(String firmwareVersion) {
        Intrinsics.checkNotNullParameter(firmwareVersion, "firmwareVersion");
        ActivityLightDeviceBinding activityLightDeviceBinding = (ActivityLightDeviceBinding) this.binding;
        TextView textView = activityLightDeviceBinding != null ? activityLightDeviceBinding.versionName : null;
        if (textView == null) {
            return;
        }
        textView.setText(firmwareVersion);
    }

    @Override
    public void onGetDeviceModel(String model) {
        Intrinsics.checkNotNullParameter(model, "model");
        super.onGetDeviceModel(model);
        ActivityLightDeviceBinding activityLightDeviceBinding = (ActivityLightDeviceBinding) this.binding;
        TextView textView = activityLightDeviceBinding != null ? activityLightDeviceBinding.modelName : null;
        if (textView == null) {
            return;
        }
        textView.setText(model);
    }

    @Override
    public void onGetDeviceBattery(int i) {
//        BuildersKt__Builders_commonKt.launch$default(LifecycleOwnerKt.getLifecycleScope(this), Dispatchers.getMain(), null, new LightDeviceActivity$onGetDeviceBattery$1(this, i, null), 2, null);
    }

    @Override
    public void onGetDeviceManufacture(String manufacture) {
        Intrinsics.checkNotNullParameter(manufacture, "manufacture");
        ActivityLightDeviceBinding activityLightDeviceBinding = (ActivityLightDeviceBinding) this.binding;
        TextView textView = activityLightDeviceBinding != null ? activityLightDeviceBinding.manufactureName : null;
        if (textView == null) {
            return;
        }
        textView.setText(manufacture);
    }

    static final class LightDeviceActivity$setLightBeam$1 implements Function0<Unit> {
        public static final LightDeviceActivity$setLightBeam$1 INSTANCE = new LightDeviceActivity$setLightBeam$1();

        LightDeviceActivity$setLightBeam$1() {
            super();
        }

        public final Unit invoke() {
            return null;
        }
    }

    @Metadata(d1 = {"\000\b\n\000\n\002\020\002\n\000\020\000\032\0020\001H\n\006\002\b\002"}, d2 = {"<anonymous>", "", "invoke"}, k = 3, mv = {1, 7, 1}, xi = 48)
    final class LightDeviceActivity$setLightBeam$2 implements Function0<Unit> {
//        final LightDeviceActivity this$0;

        LightDeviceActivity$setLightBeam$2(LightDeviceActivity lightDeviceActivity) {
            super();
        }

        public final Unit invoke() {
            LightDeviceActivity.this.toast(R.string.toast_operate_failed);
            return null;
        }
    }

    static final class LightDeviceActivity$setLightIntensity$1 implements Function0<Unit> {
        public static final LightDeviceActivity$setLightIntensity$1 INSTANCE = new LightDeviceActivity$setLightIntensity$1();

        LightDeviceActivity$setLightIntensity$1() {
            super();
        }

        public final Unit invoke() {
            return null;
        }
    }

    final class LightDeviceActivity$setLightIntensity$2 implements Function0<Unit> {
//        final LightDeviceActivity this$0;

        LightDeviceActivity$setLightIntensity$2(LightDeviceActivity lightDeviceActivity) {
            super();
        }

        public final Unit invoke() {
            LightDeviceActivity.this.toast(R.string.toast_operate_failed);
            return null;
        }
    }

    static final class LightDeviceActivity$setLightMode$1 implements Function0<Unit> {
        public static final LightDeviceActivity$setLightMode$1 INSTANCE = new LightDeviceActivity$setLightMode$1();
        LightDeviceActivity$setLightMode$1() {
            super();
        }

        public final Unit invoke() {
            return null;
        }
    }

    @Metadata(d1 = {"\000\b\n\000\n\002\020\002\n\000\020\000\032\0020\001H\n\006\002\b\002"}, d2 = {"<anonymous>", "", "invoke"}, k = 3, mv = {1, 7, 1}, xi = 48)
    final class LightDeviceActivity$setLightMode$2 implements Function0<Unit> {
//        final LightDeviceActivity this$0;
        LightDeviceActivity$setLightMode$2(LightDeviceActivity lightDeviceActivity) {
            super();
        }

        public final Unit invoke() {
            LightDeviceActivity.this.toast(R.string.toast_operate_failed);
            return null;
        }
    }

    static final class LightDeviceActivity$switchLightStateOnOrOff$1$1 implements Function0<Unit> {
        public static final LightDeviceActivity$switchLightStateOnOrOff$1$1 INSTANCE = new LightDeviceActivity$switchLightStateOnOrOff$1$1();

        LightDeviceActivity$switchLightStateOnOrOff$1$1() {
            super();
        }

        public final Unit invoke() {
            return null;
        }
    }

    final class LightDeviceActivity$switchLightStateOnOrOff$1$2 implements Function0<Unit> {
//        final LightDeviceActivity this$0;

        LightDeviceActivity$switchLightStateOnOrOff$1$2(LightDeviceActivity lightDeviceActivity) {
            super();
        }
        public final Unit invoke() {
            LightDeviceActivity.this.toast(R.string.toast_operate_failed);
            return null;
        }
    }

    static final class LightDeviceActivity$switchLightStateOnOrOff$1$3 implements Function0<Unit> {
        public static final LightDeviceActivity$switchLightStateOnOrOff$1$3 INSTANCE = new LightDeviceActivity$switchLightStateOnOrOff$1$3();

        LightDeviceActivity$switchLightStateOnOrOff$1$3() {
            super();
        }

        public final Unit invoke() {
            return null;
        }
    }

    final class LightDeviceActivity$switchLightStateOnOrOff$1$4 implements Function0<Unit> {
//        final LightDeviceActivity this$0;
        LightDeviceActivity$switchLightStateOnOrOff$1$4(LightDeviceActivity lightDeviceActivity) {
            super();
        }

        public final Unit invoke() {
            LightDeviceActivity.this.toast(R.string.toast_operate_failed);
            return null;
        }
    }

    final class LightDeviceActivity$onDeviceConnected$1 implements Function0<Unit> {
//        final LightDeviceController $deviceController;
//        final LightDeviceActivity this$0;
        LightDeviceActivity$onDeviceConnected$1(LightDeviceActivity lightDeviceActivity, LightDeviceController param1LightDeviceController) {
            super();
        }

        public final Unit invoke() {
            LightDeviceActivity.this.onDeviceValid();
//            this.$deviceController.clearDoneCallBack();
            return null;
        }
    }

}
